<template>
  <div class="lineChart" :style="'height:' + height + 'px;'">
    <div
      id="limitLine"
      class="echart"
      :style="{ height: height - 30 + 'px' }"
    ></div>
  </div>
</template>

<script>
import echarts from "@/utils/lib/echarts";
import { LineChart } from "echarts/charts";
echarts.use([LineChart]);
import colorConst from "@/utils/custom-color.js";
export default {
  name: "lineChat",
  props: {
    // 折线图数据
    lineData: {
      type: Array,
      default: [],
    },
    xaxisData: {
      type: Array,
      default: [],
    },
    height: {
      type: Number,
      default: 200,
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  watch: {
    lineData(val) {
      if (val) {
        this.chartDes();
        this.lineCharts(this.lineData);
      }
    },
  },
  mounted() {
    this.lineCharts(this.lineData);
  },
  beforeDestroy() {
    this.chartDes();
  },
  methods: {
    chartDes() {
      if (!this.chart) {
        return;
      }
      this.chart.dispose();
      this.chart = null;
    },
    lineCharts(lineData) {
      let nameArr = [];
      lineData.forEach((element) => {
        let obj = {
          name: element.name,
          icon: "roundRect",
          itemStyle: {
            opacity: 0.6,
            // color: element.lineStyle.color,
          },
        };
        nameArr.push(obj);
      });
      let option = {
        xAxis: {
          name: "(h)",
          type: "category",
          boundaryGap: false,
          data: this.xaxisData,
          axisLabel: {
            interval: 0,
          },
        },
        yAxis: {
          type: "value",
        },
        grid: {
          left: "1%",
          right: "5%",
          bottom: "1%",
          top: "15%",
          containLabel: true,
        },
        legend: {
          type: "scroll",
          width: 600,
          data: nameArr,
        },
        color: colorConst.customColorarr,
        tooltip: {
          trigger: "axis",
        },
        axisLine: {
          show: true,
          lineStyle: {
            color: "#333",
            width: "5",
            type: "solid",
          },
        },
        series: lineData,
      };
      this.initChart("limitLine", option);
    },
    initChart(id, options) {
      this.chart = echarts.init(document.getElementById(id));
      this.chart.setOption(options);
      window.addEventListener("resize", () => {
        if (this.chart) {
          this.chart.resize();
        }
      });
    },
  },
};
</script>

<style scoped lang="scss">
.lineChart {
  display: flex;
  align-items: flex-end;
  .echart {
    width: 100%;
  }
}
</style>
